// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: fullnames 
// Source File Name:   AgentWorker.java

package app;

import java.io.IOException;
import java.util.Vector;
//            ParagrahpElement, Hyperlink, ImageElement, ArabicElement, 
import javax.microedition.media.Manager;
//            ChoiceElement, Utils
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

public class SoundPlayer
    implements java.lang.Runnable
{
    private boolean running;
    private java.lang.Object lock;
    private java.util.Vector queue;
    private String playAyat;
    private Player pl;
    

    public SoundPlayer()
    {
        running = true;
        lock = this;
        queue = new Vector();
        (new Thread(this)).start();
    }

    public void stopSignal()
    {
        running = false;
        synchronized(lock)
        {
            lock.notify();
        }
    }

    public void addQueue(java.lang.String query)
    {
        if(query.equals("play")) {
            synchronized(lock)
            {
                lock.notify();
            }
        } else {
            synchronized(queue)
            {
                queue.addElement(query);
            }
        }
    }

    public void stopAll()
    {
        /*
        try {
            if(pl!=null)
                pl.stop();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
         *
         */
        synchronized(queue)
        {
            queue.removeAllElements();
        }
        
    }

    public void run()
    {
        do
        {
            if(!running)
                break;
            synchronized(queue)
            {
                if(queue.size() > 0)
                {
                    playAyat = (String) queue.elementAt(0);
                    queue.removeElementAt(0);
                }
            }
            
            if(playAyat == null)
            {
                synchronized(lock)
                {
                    try
                    {
                        wait();
                    }
                    catch(java.lang.InterruptedException e) { }
                }
                if(!running)
                    break;
            } else
            {
                try {
                    //load sound and play
                    System.out.println("Playing "+playAyat);
                    pl = null;
                    pl = Manager.createPlayer(getClass().getResourceAsStream("/sound/" + playAyat + ".wav"), "audio/x-wav");
                    pl.start();
                    long dur = (pl.getDuration()/1000) + 500;
                    try {
                        Thread.sleep(dur);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }

                    playAyat = null;

                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (MediaException ex) {
                    ex.printStackTrace();
                }
            }
        } while(true);
    }
}
